// 引入接口文件
import { findStudyDataList,findStudyMoudleList,verifyCodeFun,editContent,addOrUpdateStudyMoudle} from './talkCollegeAddServ.js'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'

// 初始默认状态
const defultState = {
  modelObj: {
    btnLoading: false,
  },
  loading: false,
  actionType: 'add',
  moudleList: [],
  nameModalObj:{
    visible: false
  },
  moudleName: '',
  query: {
    moudle: "",
    type: "",
    title: "",
    url: [],
    urlName: '',
    creater: localStorage.getItem('username'),
    ifSlideShow: "N",//#如果是轮播 那么此参数为Y 否为N
    slideUrl: '',// # 如果不是轮播 那么此参数不要填
    tableType: '0'//# 0行销 1惠客通
  }
}

// 声明module
const tmpModule = {
  namespace: 'defaultModel',
  // 默认状态
  state: cloneDeep(defultState),
  // 入口函数(先执行)
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        // 页面路由匹配时执行
        if('' + tmpModule.pathname === '' + pathname){
          // 执行业务操作
          dispatch({ type: 'updateStore', payload: { ...cloneDeep(defultState), ...query } })//每次进去页面先重置状态
          dispatch({type: 'verifyCodeFun',payload: {query}})
        } // end if
      }) // end listen
    },
  },

  // 方法列表
  effects: {
    *verifyCodeFun({payload},{put,select,call}){
      if(yield call(verifyCodeFun,{})){
        // 执行业务操作
        if(payload.query.actionType == 'edit'||payload.query.actionType == 'check'){
          yield put({type: 'queryInfo',payload: {actionType: payload.query.actionType,id: payload.query.id, }}) // end dispatch
        }
        yield put({type: 'findStudyMoudleList', payload: {actionType: payload.query.actionType,id: payload.query.id, }})
      }
    },
    //查询记录
    *queryInfo({ payload }, { put, call, select }) {
      let {query} = yield select(state => state[tmpModule.namespace])
      let params = {
        id: payload.id
      }
      if(yield call(verifyCodeFun,{})){
      const { rows,success } = yield call(findStudyDataList, {...params})
      let list = [{
        url:rows[0].url?rows[0].url:'',
        name: rows[0].urlName?rows[0].urlName:''
      }]
        query = rows[0]
        query.url = list
        yield put({type: 'updateStore', payload: {query}})
      }
    },
    //获取模块类型
    *findStudyMoudleList({payload},{put,select,call}){
      let {moudleList} = yield select(state => state[tmpModule.namespace])
      let params = {}
      if(yield call(verifyCodeFun,{})){
      const { rows,success } = yield call(findStudyMoudleList, {...params})
        if(!rows.length){
          return
        }
        moudleList =  rows.map((item,index)=>{
          return {
            label: item.moudleName,
            value: item.id,
          }
        })
        yield put({type: 'updateStore', payload: {moudleList}})
      }
      },
      *editContent({payload},{put,select,call}){
        let {query} = yield select(state => state[tmpModule.namespace])
        if(query.ifSlideShow != 'Y'){
          query.slideUrl = ''
        }
        let detil = JSON.parse(JSON.stringify(query))
        let params = detil
        params.url = query.url[0].url?query.url[0].url:''
        params.urlName = query.url[0].name?query.url[0].name:''
        const { success, msg, data } = yield call(editContent, {...params})
        if(success){
          message.success(msg)
          hashHistory.push({
            pathname: '/home/expandManage/talkCollege',
            query: {}
          })
        }
      },
      *addOrUpdateStudyMoudle({payload},{put,select,call}){
        let {moudleName} = yield select(state => state[tmpModule.namespace])
        let params = {
          moudleName: moudleName
        }
        const { success, msg, data } = yield call(addOrUpdateStudyMoudle, {...params})
        if(success){
          message.success(msg)
          yield put({type: 'findStudyMoudleList', payload: {}})
          moudleName = ''
          yield put({type: 'updateStore', payload: {moudleName}})
        }
      }

  },

  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
